#include "rental.h"
#include <ctime>

using namespace std;

Rental::Rental(const string& b_id, const string& m_id){
    time_t rawtime;

    time ( &rawtime );

    b_ID = b_id;
    m_ID = m_id;
    from = rawtime;
    to = 0;
}

ifstream& operator>>(ifstream& f, Rental& df){
    f >> df.b_ID;
    f >> df.m_ID;
    f >> df.from;
    f >> df.to;

    return f;
}

ofstream& operator<<(ofstream& f, const Rental& df){
    f << df.b_ID << " "
      << df.m_ID << " "
      << df.from << " "
      << df.to << " " << endl;

    return f;
}

ostream& operator<<(ostream& o, const Rental& df){
    struct tm * timeinfo;
    char from [15];
    char to[15];

    timeinfo = localtime ( &df.from );
    strftime (from,80,"%Y.%m.%d.",timeinfo);

    if(df.to != 0){
        timeinfo = localtime ( &df.to );
        strftime (to,80,"%Y.%m.%d.",timeinfo);
    }
    else to = {};

    o << "Book id: " << df.b_ID << endl
      << "Member id: " << df.m_ID << endl
      << "From: " << from << endl
      << "To: " << to << endl << endl;

    return o;
}

bool operator==(const Rental& af, const Rental& bf) {
    return af.getB_ID() == bf.getB_ID() && af.getM_ID() == af.getM_ID();
}
